const router = require('koa-router')()
const { query } = require("../db")
const { URL } = require("url");
const path = require("path");
const fs = require("fs");
const {httpcli} = require('gohttp')
router.prefix('/login');


router.post('/',async ctx=>{
    let data = ctx.request.body
    // console.log(ctx.request.body)
    //获取小程序端wx.login获取的授权登陆码
    let url = `https://api.weixin.qq.com/sns/jscode2session`
                + `?js_code=${data.code}`
                + `&appid=${data.appid}&secret=${data.secret}`
                + `&grant_type=authorization_code`
    
    //发起请求获取用户信息
    let ret = await httpcli.get(url)
    let r = ret.json()
    console.log(r)

    //返回信息到小程序(r.session_key与r.openid)
    ctx.res.end(JSON.stringify(r))

  
})


module.exports = router
